<!--勾选框组件,'0'不选中，'1'选中-->
<template>
  <a-form-item :label="label">
    <a-checkbox-group
        :style="getStyle"
        v-decorator="vDecorator"
        :disabled="isDisabled"
        @change="change"
    >
      <a-checkbox value="1"></a-checkbox>
    </a-checkbox-group>
  </a-form-item>
</template>

<script>
import { mixin } from './js/form.js'

export default {
  name: 'AntFormCheckbox',
  mixins: [mixin],
  data() {
    return {
      //是否禁用和输入提示，由AntForm组件调用
      cdisabled: false,
      cplaceholder: '请输入' + this.label
    }
  },
  props: {
    /**
     * 是否禁用
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * 是否由form表单自动布局
     */
    autoLayoutByForm: {
      type: Boolean,
      default: true
    },
    /**
     * 显示文本
     */
    label: {
      type: String,
      default: ''
    },
    /**
     * 绑定字段 ['phone' ,{ rules: [{ required: true},{validator:function(rule, value, callback)] }],自定义验证方法时callback回调函数必须在方法中调用，
     * 失败返回错误提示callback("xxx")，成功返回callback()
     */
    vDecorator: {
      type: Array,
      default: () => []
    },
    /**
     * css样式，优先级最高
     */
    cstyle: {
      type: String,
      default: ''
    },
    /**
     * 非自动布局的宽度，优先级最低
     */
    defaultWidth: {
      type: Number,
      default: 240
    }
  },
  computed: {
    /**
     * 获取样式
     */
    getStyle() {
      return { width: (this.percentageLayoutWidth ? this.percentageLayoutWidth : this.defaultWidth) + 'px', ...this.cstyle }
    }
  },
  created() {
  },
  mounted() {
    // 自动添加验证为空时的message
    let vDecorator = this.vDecorator
    if (vDecorator.length > 1 && vDecorator[1].rules) {
      let rules = vDecorator[1].rules
      rules.forEach(obj => {
        if (obj.required && !obj.message) {
          obj.message = '请选择' + this.label
        }
      })
    }
  },
  watch: {},
  beforeDestroy() {
  },
  methods: {
    /**
     * 选择数据发生改变事件
     * @param value
     * @param option
     */
    change(checked, event) {
      this.$emit('change', checked, event)
    },
    /**
     * 赋值之前的转换操作,由AntForm组件自动调用
     * 参数：recordPara，转换之前的record
     * 返回值：转换之后的可用于赋值的对象
     */
    beforeSetValue(recordPara) {
      let record = {}
      let key = this.vDecorator[0]
      let val = recordPara[key]
      if (val && val != 0) {
        val = ['1']
      } else {
        val = ['0']
      }
      record[key] = val
      return record
    },
    /**
     * 获取值之前的转换操作,由AntForm组件自动调用
     * 参数：recordPara，转换之前的record
     * 返回值：转换之后的对象
     */
    beforeGetValue(recordPara) {
      let record = {}
      let key = this.vDecorator[0]
      let val = recordPara[key]
      if (!val || val.length === 0) {
        val = '0'
      } else {
        val = val.join(',')
      }
      record[key] = val
      return record
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .ant-checkbox-input {
  width: 16px;
  height: 16px;
}

::v-deep .ant-checkbox-wrapper:hover .ant-checkbox-inner,
::v-deep .ant-checkbox:hover .ant-checkbox-inner,
::v-deep .ant-checkbox-input:focus + .ant-checkbox-inner {
  border-color: #1a67f8;
}
</style>
